package com.ihr360.payroll.service.payroll.convertor;

import com.google.common.collect.Lists;
import com.ihr360.commons.model.IConvertor;
import com.ihr360.payroll.config.reference.ReferenceServiceEntity;
import com.ihr360.payroll.dao.ReferenceServiceRepository;
import com.ihr360.payroll.model.dto.reference.ReferenceService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.List;

@Component
public class ReferenceServiceConvertor implements IConvertor<ReferenceServiceEntity, ReferenceService> {


    @Autowired
    private ReferenceServiceRepository referenceServiceRepository;

    @Override
    public ReferenceService convertEntityToDto(ReferenceServiceEntity entity) {
        ReferenceService dto = null;
        if (entity.getId() != null) {
            dto = referenceServiceRepository.findOne(entity.getId());
        }
        if (dto == null) {
            dto = new ReferenceService();
        }
        BeanUtils.copyProperties(entity, dto);
        return dto;
    }

    @Override
    public ReferenceServiceEntity convertDtoToEntity(ReferenceService dto) {

        ReferenceServiceEntity referenceServiceEntity = new ReferenceServiceEntity();
        BeanUtils.copyProperties(dto, referenceServiceEntity);
        return referenceServiceEntity;
    }

    public Collection<ReferenceServiceEntity> convertDtosToEntitys(Collection<ReferenceService> dtos) {
        if (CollectionUtils.isEmpty(dtos)) {
            return Lists.newArrayList();
        }
        List<ReferenceServiceEntity> entities = Lists.newArrayListWithCapacity(dtos.size());
        dtos.forEach(dto -> {
            entities.add(convertDtoToEntity(dto));
        });
        return entities;
    }
}
